/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.sql;

import java.io.Serializable;

/**
 * Criterion element (to be used in Criteria)
 * 
 * @author Laurent GUERIN
 *  
 */
public class Criterion implements Serializable
{    
	private static final long serialVersionUID = 1L;

	private final String         _sSqlExpression ;  // SQL where criterion ( ie : "agency = ?" )

    //private final int          _iParamType     ;  
    private final CriterionType  _paramType     ;  

    private final String         _sParamName     ;  

    private String  _sParamValue    = null;

    private Object  _oParamValue    = null;

    private boolean _bUsed          = false;

//    /**
//     * Creates a new Criterion object
//     * @param sSqlExpression the SQL criterion expression ( eg : "agency = ?", "code > ?", ... )
//     * @param iType the criterion parameter type ( eg : STRING, INTEGER, ... ) ( see "ParamType" )
//     */
    //public Criterion(String sSqlExpression, int iType)
    /**
     * Creates a new Criterion object
     * @param sqlExpression the SQL criterion expression ( eg : "agency = ?", "code > ?", ... )
     * @param type the criterion parameter type ( eg : STRING, INT, DATE, ... )
     */
    public Criterion(String sqlExpression, CriterionType type)
    {
        super();
        _sSqlExpression = sqlExpression ;
        _paramType = type;
        _sParamName = null;
    }

//    /**
//     * Creates a new Criterion object
//     * @param sSqlExpression the SQL criterion expression ( eg : "agency = ?", "code > ?", ... )
//     * @param iType the criterion parameter type ( eg : STRING, INTEGER, ... ) ( see "ParamType" )
//     * @param sParamName the name of the parameter associated with the criterion 
//     * @since 1.1.0
//     */
//    public Criterion(String sSqlExpression, int iType, String sParamName)
    /**
     * Creates a new Criterion object
     * @param sqlExpression the SQL criterion expression ( eg : "agency = ?", "code > ?", ... )
     * @param type the criterion parameter type ( eg : STRING, INT, DATE, ... )
     * @param paramName the name of the parameter associated with the criterion
     */
    public Criterion(String sqlExpression, CriterionType type, String paramName)
    {
        super();
        _sSqlExpression = sqlExpression ;
        _paramType = type;
        _sParamName = paramName;
    }

    /**
     * Returns the SQL expression of this criterion ( eg : "agency = ?", "name like ?", ... )
     * @return
     */
    public String getSqlExpression()
    {
        return _sSqlExpression;
    }

    /**
     * Returns the type of the parameter associated with this criterion <br>
     * @return
     */
//    public int getParamType()
    public CriterionType getParamType()
    {
        return _paramType;
    }

    /**
     * Returns the name of the parameter associated with this criterion <br>
     * @return
     */
    public String getParamName()
    {
        return _sParamName ;
    }

    /**
     * Returns the parameter value associated with this criterion 
     * @return
     */
    public String getParamValueAsString()
    {
        return _sParamValue ;
    }

    /**
     * Returns the parameter value associated with this criterion 
     * @return
     */
    public Object getParamValueAsObject()
    {
        return _oParamValue ;
    }

//    /**
//     * Declares this criterion as to be used with the given parameter value
//     * @param sParamVal
//     */
//    public void useWithValue(String sParamVal)
//    {
//        _oParamValue = null ;
//        _sParamValue = null ;
//        try
//        {
//            _oParamValue = Cast.toObject(sParamVal, _iParamType) ;
//            _sParamValue = sParamVal;
//        } catch (TelosysException e)
//        {
//            throw new TelosysRuntimeException("Criteria : useWithValue(" + sParamVal + ") invalid parameter, type = " + _iParamType );
//        }
//        _bUsed = true;
//    }
    /**
     * Declares this criterion as to be used with the given parameter value
     * @param paramValue
     */
    public void useWithValue(String paramValue)
    {
        _oParamValue = _paramType.toObject(paramValue);
        _sParamValue = paramValue;
        _bUsed = true;
    }


    /**
     * Declare this criterion as not to be used 
     */
    public void doNotUse()
    {
        _oParamValue = null ;
        _sParamValue = null;
        _bUsed = false;
    }

    /**
     * Returns true if this criterion is used in the SQL request
     * @return
     */
    public boolean isUsed()
    {
        return _bUsed ;
    }
}